package user_handler

import (
	"net/http"

	"bird/internal/pkg/code"
	"bird/internal/pkg/core"
	"bird/internal/pkg/validation"
	"bird/pkg/errno"
)

type verifyCodeRequest struct {
	PhoneNo string `json:"phone_no" form:"phone_no"` //手机号码
}

type verifyCodeResponse struct {
	VCode string `json:"vcode"` //验证码
}

// VerifyCode 验证码接口
// @Summary 验证码接口
// @Description 验证码接口
// @Tags API.user
// @Accept json
// @Produce json
// @Param Request body verifyCodeRequest true "请求信息"
// @Success 200 {object} verifyCodeResponse
// @Failure 400 {object} code.Failure
// @Router /bird/api/verify_code [get]
func (h *handler) VerifyCode() core.HandlerFunc {
	return func(c core.Context) {
		req := new(verifyCodeRequest)
		res := new(verifyCodeResponse)

		if err := c.ShouldBindForm(req); err != nil {
			c.AbortWithError(errno.NewError(
				http.StatusBadRequest,
				code.ParamBindError,
				validation.Error(err)).WithErr(err),
			)
			return
		}
		vcode, err := h.userService.VerifyCode(c, req.PhoneNo)
		if err != nil {
			c.AbortWithError(errno.NewError(
				http.StatusOK,
				code.SmsSendError,
				code.Text(code.SmsSendError)).WithErr(err),
			)
			return
		}

		res.VCode = vcode
		c.Payload(res)
	}
}
